﻿#region Microsoft Reciprocal License (Ms-RL)
/*
				This license governs use of the accompanying software. If you use the software, you accept this license. If you do not accept the license, do not use the software.

				1. Definitions
				The terms "reproduce," "reproduction," "derivative works," and "distribution" have the same meaning here as under U.S. copyright law. A "contribution" is the original software, or any additions or changes to the software. A "contributor" is any person that distributes its contribution under this license. "Licensed patents" are a contributor's patent claims that read directly on its contribution.

				2. Grant of Rights
				(A) Copyright Grant- Subject to the terms of this license, including the license conditions and limitations in section 3, each contributor grants you a non-exclusive, worldwide, royalty-free copyright license to reproduce its contribution, prepare derivative works of its contribution, and distribute its contribution or any derivative works that you create.
				(B) Patent Grant- Subject to the terms of this license, including the license conditions and limitations in section 3, each contributor grants you a non-exclusive, worldwide, royalty-free license under its licensed patents to make, have made, use, sell, offer for sale, import, and/or otherwise dispose of its contribution in the software or derivative works of the contribution in the software.

				3. Conditions and Limitations
				(A) Reciprocal Grants- For any file you distribute that contains code from the software (in source code or binary format), you must provide recipients the source code to that file along with a copy of this license, which license will govern that file. You may license other files that are entirely your own work and do not contain code from the software under any terms you choose.
				(B) No Trademark License- This license does not grant you rights to use any contributors' name, logo, or trademarks.
				(C) If you bring a patent claim against any contributor over patents that you claim are infringed by the software, your patent license from such contributor to the software ends automatically.
				(D) If you distribute any portion of the software, you must retain all copyright, patent, trademark, and attribution notices that are present in the software.
				(E) If you distribute any portion of the software in source code form, you may do so only under this license by including a complete copy of this license with your distribution. If you distribute any portion of the software in compiled or object code form, you may only do so under a license that complies with this license.
				(F) The software is licensed "as-is." You bear the risk of using it. The contributors give no express warranties, guarantees or conditions. You may have additional consumer rights under your local laws which this license cannot change. To the extent permitted under your local laws, the contributors exclude the implied warranties of merchantability, fitness for a particular purpose and non-infringement.
				*/
#endregion

using System;
using System.Collections.Generic;

namespace Anheledir.XmlDialogEditor {
  enum XmlDialogView {
    Listview,
    Treeview
  }

  enum StatusCode {
    note,
    warn,
    error
  }

  public class XmlDialogEntry : IDisposable {
    #region Properties

    private static int _c = 10;
    private int _uid, _id, _prepause, _pause, _hue;
    private bool _lockOn, _allowNpc, _ignoreCar;
    private string _keywords, _text, _condition, _action, _gump, _depends;

    public int UID {
      get { return _uid; }
    }
    public int Hue {
      get { return _hue; }
      set {
        if ( value > 37852 ) {
          _hue = 37852;
        } else if ( value < -1 ) {
          _hue = -1;
        } else {
          _hue = value;
        }
      }
    }
    public int Pause {
      get { return _pause; }
      set { _pause = value; }
    }
    public int Prepause {
      get { return _prepause; }
      set { _prepause = value; }
    }
    public int Id {
      get { return _id; }
      set {
        if ( value > 0 ) {
          _id = value;
        }
      }
    }
    public string Depends {
      get { return _depends; }
      set { _depends = value; }
    }
    public bool AllowNpc {
      get { return _allowNpc; }
      set { _allowNpc = value; }
    }
    public bool LockOn {
      get { return _lockOn; }
      set { _lockOn = value; }
    }
    public bool IgnoreCar {
      get { return _ignoreCar; }
      set { _ignoreCar = value; }
    }
    public string Gump {
      get { return _gump; }
      set { _gump = value; }
    }
    public string Action {
      get { return _action; }
      set { _action = value; }
    }
    public string Condition {
      get { return _condition; }
      set { _condition = value; }
    }
    public string Text {
      get { return _text; }
      set { _text = value; }
    }
    public string Keywords {
      get { return _keywords; }
      set { _keywords = value; }
    }

    #endregion

    #region Constructors

    public XmlDialogEntry() {
      _uid = _c;
      _id = _uid;
      _prepause = -1;
      _pause = 1;
      _hue = -1;
      _lockOn = true;
      _allowNpc = false;
      _ignoreCar = false;
      _depends = _keywords = _text = _condition = _action = _gump = "";
      _c = _c + 10;
    }
    public XmlDialogEntry(bool temp) {
      _uid = _c;
      _id = _uid;
      _prepause = -1;
      _pause = 1;
      _hue = -1;
      _lockOn = true;
      _allowNpc = false;
      _ignoreCar = false;
      _depends = _keywords = _text = _condition = _action = _gump = "";
      if ( !temp ) {
        _c = _c + 10;
      }
    }
    public XmlDialogEntry( int id ) {
      _uid = _c;
      _id = id;
      _prepause = -1;
      _pause = 1;
      _hue = -1;
      _lockOn = true;
      _allowNpc = false;
      _ignoreCar = false;
      _depends = _keywords = _text = _condition = _action = _gump = "";
      _c = _c + 10;
    }
    public XmlDialogEntry( int uid, int id ) {
      _uid = uid;
      _id = id;
      _prepause = -1;
      _pause = 1;
      _hue = -1;
      _lockOn = true;
      _allowNpc = false;
      _ignoreCar = false;
      _depends = _keywords = _text = _condition = _action = _gump = "";
    }
    public XmlDialogEntry( int id, int prePause, string depends, int pause, int hue, bool lockOn, bool allowNpc, bool ignoreCar, string keywords, string text, string condition, string action, string gump ) {
      _uid = _c;
      _id = id;
      _depends = depends;
      _prepause = prePause;
      _pause = pause;
      _hue = hue;
      _lockOn = lockOn;
      _allowNpc = allowNpc;
      _ignoreCar = ignoreCar;
      _keywords = keywords;
      _text = text;
      _condition = condition;
      _action = action;
      _gump = gump;
      _c = _c + 10;
    }

    #endregion

    #region Functions

    public override string ToString() {
      string str = "ID " + _id.ToString();
      if ( _ignoreCar ) {
        str += "*";
      }
      if ( _keywords != string.Empty ) {
        str += ", " + languages.Keywords + ": " + _keywords;
      } else if ( _condition != string.Empty ) {
        str += ", " + languages.Condition + ": " + _condition;
      }
      return str;
    }

    internal static void ResetCounter() {
      _c = 10;
    }

    #endregion

    #region IDisposable Members

    void IDisposable.Dispose() {
      //throw new NotImplementedException();
    }

    #endregion
  }

  public class XmlDialog : IDisposable {
    #region Properties

    private string _name, _trigOnCarried, _noTrigOnCarried;
    private int _proximityRange, _speechPace, _resetTime;
    private bool _ghostTrig;
    private List<XmlDialogEntry> _entries;

    public string NoTrigOnCarried {
      get { return _noTrigOnCarried; }
      set { _noTrigOnCarried = value; }
    }
    public string TrigOnCarried {
      get { return _trigOnCarried; }
      set { _trigOnCarried = value; }
    }
    public string Name {
      get { return _name; }
      set { _name = value; }
    }
    public int ResetTime {
      get { return _resetTime; }
      set { _resetTime = value; }
    }
    public int SpeechPace {
      get { return _speechPace; }
      set { _speechPace = value; }
    }
    public int ProximityRange {
      get { return _proximityRange; }
      set { _proximityRange = value; }
    }
    public bool GhostTrig {
      get { return _ghostTrig; }
      set { _ghostTrig = value; }
    }
    internal List<XmlDialogEntry> Entries {
      get { return _entries; }
      set { _entries = value; }
    }

    #endregion

    #region Constructors

    public XmlDialog() {
      _name = _trigOnCarried = _noTrigOnCarried = "";
      _proximityRange = 3;
      _speechPace = 10;
      _ghostTrig = false;
      _entries = new List<XmlDialogEntry>();
      _entries.Add( new XmlDialogEntry( 0 ) );
      _resetTime = 60;
    }
    public XmlDialog( bool empty) {
      _name = _trigOnCarried = _noTrigOnCarried = "";
      _proximityRange = 3;
      _speechPace = 10;
      _ghostTrig = false;
      _entries = new List<XmlDialogEntry>();
      if ( !empty ) {
        _entries.Add( new XmlDialogEntry( 0 ) );
      }
      _resetTime = 60;
    }
    public XmlDialog( string name, string trigOnCarried, string noTrigOnCarried, int proximityRange, int speechPace, bool ghostTrig, List<XmlDialogEntry> entries, int resetTime ) {
      _name = name;
      _trigOnCarried = trigOnCarried;
      _noTrigOnCarried = noTrigOnCarried;
      _proximityRange = proximityRange;
      _speechPace = speechPace;
      _ghostTrig = ghostTrig;
      _entries = entries;
      _resetTime = resetTime;
    }
    public XmlDialog( string name ) {
      _name = name;
      _proximityRange = 3;
      _speechPace = 10;
      _ghostTrig = false;
      _entries = new List<XmlDialogEntry>();
      _entries.Add( new XmlDialogEntry( 0 ) );
      _resetTime = 60;
    }

    #endregion

    #region Functions

    public bool DeleteEntry( int uid ) {
      if ( uid > 0 ) {
        foreach ( XmlDialogEntry de in _entries ) {
          if ( de.UID == uid ) {
            _entries.Remove( de );
            return true;
          }
        }
      }
      return false;
    }

    public bool DupeEntry( int uid ) {
      if ( uid > 0 ) {
        foreach ( XmlDialogEntry de in _entries ) {
          if ( de.UID == uid ) {
            _entries.Add( new XmlDialogEntry( de.Id, de.Prepause, de.Depends, de.Pause, de.Hue, de.LockOn, de.AllowNpc, de.IgnoreCar, de.Keywords, de.Text, de.Condition, de.Action, de.Gump ) );
            return true;
          }
        }
      }
      return false;
    }

    public XmlDialogEntry GetEntry( int uid ) {
      if ( uid > 0 ) {
        foreach ( XmlDialogEntry de in _entries ) {
          if ( de.UID == uid ) {
            return de;
          }
        }
      }
      return null;
    }

    public void UpdateEntry( int uid, XmlDialogEntry newValues ) {
      if ( uid > 0 ) {
        foreach ( XmlDialogEntry de in _entries ) {
          if ( de.UID == uid ) {
            de.Id = newValues.Id;
            de.Depends = newValues.Depends;
            de.Prepause = newValues.Prepause;
            de.Pause = newValues.Pause;
            de.Hue = newValues.Hue;
            de.LockOn = newValues.LockOn;
            de.AllowNpc = newValues.AllowNpc;
            de.IgnoreCar = newValues.IgnoreCar;
            de.Keywords = newValues.Keywords;
            de.Text = newValues.Text;
            de.Condition = newValues.Condition;
            de.Action = newValues.Action;
            de.Gump = newValues.Gump;
          }
        }
      }
    }

    public int AddEntry(int id, string dependsOn, int prePause, int pause, int hue, bool lockOn, bool allowNpc, bool ignoreCar, string keywords, string text, string condition, string action, string gump) {
      _entries.Add(new XmlDialogEntry(id, prePause, dependsOn, pause, hue, lockOn, allowNpc, ignoreCar, keywords, text, condition, action, gump));
      return _entries[_entries.Count - 1].UID;
    }

    public int AddEntry() {
      _entries.Add( new XmlDialogEntry() );
      return _entries[_entries.Count - 1].UID;
    }

    #endregion

    #region IDisposable Members

    void IDisposable.Dispose() {
      //throw new NotImplementedException();
    }

    #endregion
  }
}
